package cn.pangza.user.excel;

import cn.pangza.mybatisplus.entity.PageRequest;
import cn.pangza.mybatisplus.entity.PageResult;
import cn.pangza.user.entity.UserEntity;
import cn.pangza.user.service.UserService;
import cn.pangza.web.utils.excel.BaseEasyExcelExport;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Component
public class DemoExcelExport extends BaseEasyExcelExport<UserEntity> {

    @Resource
    UserService userService;

    public void exportData(String fileName, Map<String, Object> conditions) {
        this.exportExcel(fileName, conditions);
    }

    @Override
    protected List<List<String>> getExcelHead() {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("用户名"));
        head.add(Collections.singletonList("密码"));
        return head;
    }

    @Override
    protected Long dataTotalCount(Map<String, Object> conditions) {
        return userService.count();
    }

    @Override
    protected Long eachSheetTotalCount() {
        return 5000L;
    }

    @Override
    protected Long eachTimesWriteSheetTotalCount() {
        return 2000L;
    }

    @Override
    protected void buildDataList(List<List<String>> resultList, Map<String, Object> queryCondition, Long pageNo, Long pageSize) {
        PageRequest<UserEntity> pageRequest = new PageRequest<>();
        pageRequest.setPageNum(Math.toIntExact(pageNo));
        pageRequest.setPageSize(Math.toIntExact(pageSize));
        PageResult<UserEntity> userPage = userService.getUserPage(pageRequest);
        if(!CollectionUtils.isEmpty(userPage.getList())) {
            userPage.getList().forEach(user -> {
                resultList.add(Arrays.asList(user.getUsername(), user.getPassword()));
            });
        }
    }
}
